package com.maple.p2pclient.ui;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.maple.p2pclient.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MessageAdapter extends ArrayAdapter<String> {

    private Context context;

    private List<String> dataList;


    public List<String> getDataList() {
        return dataList;
    }

    public MessageAdapter(@NonNull Context context,List<String> list) {
        super(context, 0);
        this.context = context;
        this.dataList = list;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }


    public void addItem(String message){
        dataList.add(message);
        this.notifyDataSetChanged();
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//        return super.getView(position, convertView, parent);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_message,null);
            holder = new ViewHolder();
            holder.tvMessage = convertView.findViewById(R.id.tv_message);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvMessage.setText(dataList.get(position));
        return convertView;
    }

    static class ViewHolder{
        TextView tvMessage;
    }
}
